<?php
	include('site/banner.php');
	$tplHome 	= new XTemplate('view/layout.htm');
	$c			= _POST('c');
	$a			= _POST('a');
	$tplHome->assign('baseUrl', $baseUrl);
	$sql = 'SELECT * FROM tblcategory WHERE 1=1';
	$categories = $db->fetchAll($sql);
	
	$tree = creatTree($categories,'', '?c=category&a=list&id');
	$tplHome->assign('tree', $tree);
	//slider
	$sql = "SELECT * FROM tblproduct WHERE at_slider =1 ";
	$products = $db->fetchAll($sql);

	foreach ( $products as $product ) {
		
		$image = $db->fetchOne("SELECT img_url FROM tblproduct_img WHERE product_id =".$product['product_id']."  AND basic_img =1");
		$fileName = resizeImage(dirname(__FILE__).$image['img_url'], 100, 100, dirname(__FILE__).'/media/product/images/153x114/carousel-'.$product['product_id']);
		$product['imgurl'] = '/media/product/images/153x114/'.$fileName;
		
		$tplHome->insert_loop('HOME.CAROUSEL.LISTSLIDER', array('LISTSLIDER'=>$product));
	}
	$tplHome->assign('banner', $banner);
	if($c){
		include('site/'.$c.'/'.$a.'.php');
		if($_SESSION['category_name']){
			$breadcrumb = '<li class="home">
								<a title="trang chủ" href="'.$baseUrl.'">Trang chủ</a> / 
							</li>'.$_SESSION['category_name'];
			$tplHome->assign('breadcrumb', $breadcrumb);
		}
		$tplHome->parse('HOME.BREADCRUMBS');
		
	}else{
		if(count($products)>2){
			$tplHome->parse('HOME.CAROUSEL');
		}
		include('site/homepage.php');
		$tplHome->parse('HOME.BANNER');
	}
	//link
	$sql = "SELECT * FROM tblwebinfo WHERE 1=1";
	$contactresult = $db->fetchAll($sql);
	foreach($contactresult as $result){
		if($result['type']=='fb'){
			$tplHome->assign('fb', $result['content']);
			$tplHome->parse('HOME.fb');		
		}
		if($result['type']=='sky'){
			$tplHome->assign('sky', $result['content']);
			$tplHome->parse('HOME.sky');		
		}
		if($result['type']=='yahoo'){
			$tplHome->assign('yahoo', $result['content']);
			$tplHome->parse('HOME.yahoo');		
		}
		if($result['type']=='background'){
			$background = $result['content'];
				
		}
	}
	if($background){
		$tplHome->assign('background', $background);
		$tplHome->parse('HOME.BACKGROUND');	
	}
	//Best Seller
	$bestsellers = $db->fetchAll("SELECT * FROM tblproduct WHERE bestseller=1 ORDER BY RAND() LIMIT 0,5");
	foreach($bestsellers as $bestseller){
		$image 		= $db->fetchOne("SELECT img_url FROM tblproduct_img WHERE product_id =".$bestseller['product_id']."  AND basic_img =1");
		if($bestseller['price'])
			$bestseller['price'] = formatPrice($bestseller['price']) . ' đ';
		$bestseller['imgurl'] = '/media/product/images/base/no-image.png';
		
		if($image['img_url']){
			$fileName = resizeImage( dirname(__FILE__).$image['img_url'], 100, 100, dirname(__FILE__).'/media/product/images/100x100/slider-left-'.$bestseller['product_id']);
			$bestseller['imgurl'] = '/media/product/images/100x100/'.$fileName;
		}
		$tplHome->insert_loop('HOME.CATEGORY.BESTSELLER.LISTBESTSELLER', array('LISTBESTSELLER'=>$bestseller));
	}
	if(count($bestsellers)>0){
		$tplHome->parse('HOME.CATEGORY.BESTSELLER');
	}
	
	if(($c!='product' || $c=='product') && $a!='view' && $c!='news' && $a!='contact'){
		$tplHome->parse('HOME.CATEGORY');
		
	}
	
	$title	 	.= 'Topkids fashion Việt Nam';
	$tplHome->assign('title', $title);
	$tplHome->assign('content', $content);
	
	$tplHome->parse('HOME');
	$tplHome->out('HOME');